package edu.umuc.cmsc495_final_project;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;

public class keypadPanel extends JFrame {//JInternalFrame {

	private DataBase db;

	private JButton backspaceButton = null;
	private JButton loginNumbers[] = null;
	private JButton clearButton = null;
	protected JButton doneButton = null;
	protected JPasswordField passwordField;
	private String passkey = "";

	public keypadPanel(final DataBase db) {
		this.db = db;

		System.out.println("opening keypad");

		// Internal Frame - User login frame
		setTitle("User login");
		setSize(445, 620);
		setVisible(true);
		super.setAlwaysOnTop(true); 
		setResizable(false);
		new centerScreen(this);
		//((javax.swing.plaf.basic.BasicInternalFrameUI) getUI()).setNorthPane(null); // removes/undecorates title
		// bar
		getContentPane().setLayout(null);

		drawKeypadPanel();

	}

	public void closePanel() {
		dispose();
	}

	public void drawKeypadPanel() {
		// password text box
		passwordField = new JPasswordField();
		passwordField.setColumns(10);
		passwordField.setBounds(20, 20, 400, 91);
		passwordField.setVisible(true);
		passwordField.setEchoChar('*');
		passwordField.setEditable(false);
		passwordField.setFont(new Font("Verdana", Font.BOLD, 60));
		getContentPane().add(passwordField);

		// number buttons
		loginNumbers = new JButton[10];
		{
			int xLoc = -80, yLoc = 20, y = 0;
			for (int i = 0; i < 10; i++) {
				if (i % 3 == 0) {
					y += 100;
					xLoc = -80;
				}
				if (i == 9) {
					loginNumbers[i] = new JButton("0");
					loginNumbers[i].setBounds(xLoc += 200, yLoc + y, 91, 91);
				} else {
					loginNumbers[i] = new JButton(Integer.toString(i + 1));
					loginNumbers[i].setBounds(xLoc += 100, yLoc + y, 91, 91);
				}
				getContentPane().add(loginNumbers[i]);
				final int buttonIndex = i;
				loginNumbers[i].addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae2) {
						String temp = getPasskey();
						switch (buttonIndex + 1) {
						case 1:
							setPasskey(temp + "1");
							break;
						case 2:
							setPasskey(temp + "2");
							break;
						case 3:
							setPasskey(temp + "3");
							break;
						case 4:
							setPasskey(temp + "4");
							break;
						case 5:
							setPasskey(temp + "5");
							break;
						case 6:
							setPasskey(temp + "6");
							break;
						case 7:
							setPasskey(temp + "7");
							break;
						case 8:
							setPasskey(temp + "8");
							break;
						case 9:
							setPasskey(temp + "9");
							break;
						case 10:
							setPasskey(temp + "0");
							break;
						}
						// System.out.println("passkey is " + getPasskey());
						passwordField.setText(getPasskey());
					}
				});
			}
		}

		// Backspace button
		backspaceButton = new JButton("Back");
		backspaceButton.setBounds(330, 120, 91, 50);
		getContentPane().add(backspaceButton);

		backspaceButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (getPasskey().length() > 0)
					setPasskey(getPasskey().substring(0,
							getPasskey().length() - 1));

				passwordField.setText(getPasskey());
			}
		});

		// Clear button
		clearButton = new JButton("Clear");
		clearButton.setBounds(330, 180, 91, 50);
		getContentPane().add(clearButton);

		clearButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				reset();
			}
		});

		// Done button
		doneButton = new JButton("Done");
		doneButton.setBounds(330, 525, 91, 50);
		getContentPane().add(doneButton);
	}

	// to be moved to the passkey object class
	protected String getPasskey() {
		return passkey;
	}

	private void setPasskey(String passkey) {
		this.passkey = passkey;
	}

	public void reset() {
		setPasskey("");
		passwordField.setText("");
	}
}
